/**
 * <p>Title: ServiceZMCMA0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 下午02:30:43  贾雪民  Create
 *</pre>
 */
package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBase;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBaseHis;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBasicInfo;
import com.baosight.sczx.zm.cm.domain.ZMCMChangepriceHis;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfo;

/**物资调价管理
 * <p>Title: ServiceZMCMA0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 下午02:30:43  贾雪民  Create
 *</pre>
 */
public class ServiceZMCMA0 extends ServiceEPBase{

	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		outInfo.set("orgCode",UserSession.getInSessionProperty("orgCode"));//当前用户所属单位ID
		outInfo.set("orgName",UserSession.getInSessionProperty("orgName"));//当前用户所属单位
		outInfo.set("userId", UserSession.getUserId());//当前登录用户ID	
		
		Map<String, String> params = new HashMap<String, String>();
		List<Map> result = MaterialBaseHis.getAutoBaseMonth("ZMCMMaterialBaseHis.autoBaseMonth", params);
		int fdMonth = 1;
		if(result.get(0).get("fdMonth") !=null)
		{
			fdMonth = Integer.parseInt(result.get(0).get("fdMonth").toString());
			if(fdMonth == 12)
			{
				fdMonth = 1;
			}
		else 
			{
				fdMonth = fdMonth + 1;
			}
		}
		
		outInfo.set("autoBaseMonth", fdMonth);//自动结转日期
		return outInfo;
	}
	
	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
//		String fdMaterialId = inInfo.getString("fdMaterialId");
//		StaticTools.setQueryParameters(inInfo, fdMaterialId, fdMaterialId, true);//添加查询条件		
		EiInfo outInfo = super.query(inInfo, "ZMCMChangepriceHis.query",new ZMCMChangepriceHis());
		return outInfo;
	}
	
	/**新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验物资资料是否重复
		boolean validate = validate(inInfo, "ZMCMChangepriceHis.validate", new HashMap<String, String>());
		if(validate) {
			return inInfo;
		}
//		inInfo = ImportMaterialDetail.queryRecord(inInfo); //查询入库是否存在该物资记录
//		if(inInfo.getStatus()==-1){
//			return inInfo;
//		}
//		inInfo = ExportmaterialDetail.queryRecord(inInfo); //查询出库是否存在该物资记录
//		if(inInfo.getStatus()==-1){
//			return inInfo;
//		}		
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_ChangepriceHis"));//主键
		EiInfo outInfo = super.insert(inInfo, "ZMCMChangepriceHis.insert");
		return outInfo;
	}
	
	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCMChangepriceHis.update");
		return outInfo;
	}
	
	/**删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo,"ZMCMChangepriceHis.delete","result");
		EiInfo outInfo = this.query(inInfo);		
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**数据生效方法
	 * <p>Function: confirm</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMA0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30 17:38:25  贾雪民  Create
	 * </p>
	 *
	 */
	public EiInfo confirm(EiInfo inInfo) {
		inInfo = MaterialBase.updateValue(inInfo); //根据调价结果修改库存账
		inInfo = MaterialBasicInfo.updateValue(inInfo);//根据调价结果修改物资基础信息单价
		super.update(inInfo, "ZMCMChangepriceHis.confirmupdate");//修改记录状态为“生效”状态
		EiInfo outInfo = this.query(inInfo);		
		return outInfo;
	}
	
	/**
	 *
	 * 校验物资资料重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28下午02:35:57 贾雪民
	 * </p>
	 *
	 */	
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		params.put("fdMaterialId", inInfo.getBlock("result").getCellStr(0, "fdMaterialId"));

		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("已存在该物资的调价记录，不可再次调价！");
			return true;
		}
		return false;
	}
	
	/**
	 * 明细customview 取物资信息
	 * <p>Function: queryforCustomview</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21 上午09:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo materialcode(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.queryForMaterialcode1",new ZMCMMaterialBasicInfo());
		return outInfo;
	}
}
